route.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { NextRequest, NextResponse } from "next/server";
  2. import { STORAGE_KEY, internalWhiteWebDavDomains } from "../../../constant";
  3. import { getServerSideConfig } from "@/app/config/server";
  4. const config = getServerSideConfig();
  5. const mergedWhiteWebDavDomains = [
  6. ...internalWhiteWebDavDomains,
  7. ...config.whiteWebDevDomains,
  8. ].filter((domain) => Boolean(domain.trim()));
  9. async function handle(
  10. req: NextRequest,
  11. { params }: { params: { path: string[] } },
  12. ) {
  13. if (req.method === "OPTIONS") {
  14. return NextResponse.json({ body: "OK" }, { status: 200 });
  15. }
  16. const folder = STORAGE_KEY;
  17. const fileName = `${folder}/backup.json`;
  18. const requestUrl = new URL(req.url);
  19. let endpoint = requestUrl.searchParams.get("endpoint");
  20. // Validate the endpoint to prevent potential SSRF attacks
  21. if (
  22. !mergedWhiteWebDavDomains.some((domain) => endpoint?.startsWith(domain))
  23. ) {
  24. return NextResponse.json(
  25. {
  26. error: true,
  27. msg: "Invalid endpoint",
  28. },
  29. {
  30. status: 400,
  31. },
  32. );
  33. }
  34. if (!endpoint?.endsWith("/")) {
  35. endpoint += "/";
  36. }
  37. const endpointPath = params.path.join("/");
  38. const targetPath = `${endpoint}/${endpointPath}`;
  39. // only allow MKCOL, GET, PUT
  40. if (req.method !== "MKCOL" && req.method !== "GET" && req.method !== "PUT") {
  41. return NextResponse.json(
  42. {
  43. error: true,
  44. msg: "you are not allowed to request " + targetPath,
  45. },
  46. {
  47. status: 403,
  48. },
  49. );
  50. }
  51. // for MKCOL request, only allow request ${folder}
  52. if (req.method === "MKCOL" && !targetPath.endsWith(folder)) {
  53. return NextResponse.json(
  54. {
  55. error: true,
  56. msg: "you are not allowed to request " + targetPath,
  57. },
  58. {
  59. status: 403,
  60. },
  61. );
  62. }
  63. // for GET request, only allow request ending with fileName
  64. if (req.method === "GET" && !targetPath.endsWith(fileName)) {
  65. return NextResponse.json(
  66. {
  67. error: true,
  68. msg: "you are not allowed to request " + targetPath,
  69. },
  70. {
  71. status: 403,
  72. },
  73. );
  74. }
  75. // for PUT request, only allow request ending with fileName
  76. if (req.method === "PUT" && !targetPath.endsWith(fileName)) {
  77. return NextResponse.json(
  78. {
  79. error: true,
  80. msg: "you are not allowed to request " + targetPath,
  81. },
  82. {
  83. status: 403,
  84. },
  85. );
  86. }
  87. const targetUrl = `${endpoint}/${endpointPath}`;
  88. const method = req.method;
  89. const shouldNotHaveBody = ["get", "head"].includes(
  90. method?.toLowerCase() ?? "",
  91. );
  92. const fetchOptions: RequestInit = {
  93. headers: {
  94. authorization: req.headers.get("authorization") ?? "",
  95. },
  96. body: shouldNotHaveBody ? null : req.body,
  97. redirect: "manual",
  98. method,
  99. // @ts-ignore
  100. duplex: "half",
  101. };
  102. const fetchResult = await fetch(targetUrl, fetchOptions);
  103. console.log(
  104. "[Any Proxy]",
  105. targetUrl,
  106. {
  107. status: fetchResult.status,
  108. statusText: fetchResult.statusText,
  109. },
  110. fetchResult,
  111. );
  112. return fetchResult;
  113. }
  114. export const PUT = handle;
  115. export const GET = handle;
  116. export const OPTIONS = handle;
  117. export const runtime = "edge";